home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / fs / inode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  11.3 KB  |  493 lines

  1. /*
  2.  *  linux/fs/inode.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  *
  6.  * This file is subject to the terms and conditions of the GNU General Public
  7.  * License.  See the file README.legal in the main directory of this archive
  8.  * for more details.
  9.  */
  10.  
  11. #include <linux/stat.h>
  12. #include <linux/sched.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mm.h>
  15. #include <linux/string.h>
  16.  
  17. #include <asm/system.h>
  18.  
  19. static struct inode * hash_table[NR_IHASH];
  20. static struct inode * first_inode;
  21. static struct wait_queue * inode_wait = NULL;
  22. static int nr_inodes = 0, nr_free_inodes = 0;
  23.  
  24. static inline int const hashfn(dev_t dev, unsigned int i)
  25. {
  26.     return (dev ^ i) % NR_IHASH;
  27. }
  28.  
  29. static inline struct inode ** const hash(dev_t dev, int i)
  30. {
  31.     return hash_table + hashfn(dev, i);
  32. }
  33.  
  34. static void insert_inode_free(struct inode *inode)
  35. {
  36.     inode->i_next = first_inode;
  37.     inode->i_prev = first_inode->i_prev;
  38.     inode->i_next->i_prev = inode;
  39.     inode->i_prev->i_next = inode;
  40.     first_inode = inode;
  41. }
  42.  
  43. static void remove_inode_free(struct inode *inode)
  44. {
  45.     if (first_inode == inode)
  46.         first_inode = first_inode->i_next;
  47.     if (inode->i_next)
  48.         inode->i_next->i_prev = inode->i_prev;
  49.     if (inode->i_prev)
  50.         inode->i_prev->i_next = inode->i_next;
  51.     inode->i_next = inode->i_prev = NULL;
  52. }
  53.  
  54. void insert_inode_hash(struct inode *inode)
  55. {
  56.     struct inode **h;
  57.     h = hash(inode->i_dev, inode->i_ino);
  58.  
  59.     inode->i_hash_next = *h;
  60.     inode->i_hash_prev = NULL;
  61.     if (inode->i_hash_next)
  62.         inode->i_hash_next->i_hash_prev = inode;
  63.     *h = inode;
  64. }
  65.  
  66. static void remove_inode_hash(struct inode *inode)
  67. {
  68.     struct inode **h;
  69.     h = hash(inode->i_dev, inode->i_ino);
  70.  
  71.     if (*h == inode)
  72.         *h = inode->i_hash_next;
  73.     if (inode->i_hash_next)
  74.         inode->i_hash_next->i_hash_prev = inode->i_hash_prev;
  75.     if (inode->i_hash_prev)
  76.         inode->i_hash_prev->i_hash_next = inode->i_hash_next;
  77.     inode->i_hash_prev = inode->i_hash_next = NULL;
  78. }
  79.  
  80. static void put_last_free(struct inode *inode)
  81. {
  82.     remove_inode_free(inode);
  83.     inode->i_prev = first_inode->i_prev;
  84.     inode->i_prev->i_next = inode;
  85.     inode->i_next = first_inode;
  86.     inode->i_next->i_prev = inode;
  87. }
  88.  
  89. void grow_inodes(void)
  90. {
  91.     struct inode * inode;
  92.     unsigned int i;
  93.  
  94.     if (!(inode = (struct inode*) get_free_page(GFP_KERNEL)))
  95.         return;
  96.  
  97.     i=PAGE_SIZE / sizeof(struct inode);
  98.     nr_inodes += i;
  99.     nr_free_inodes += i;
  100.  
  101.     if (!first_inode)
  102.         inode->i_next = inode->i_prev = first_inode = inode++, i--;
  103.  
  104.     for ( ; i ; i-- )
  105.         insert_inode_free(inode++);
  106. }
  107.  
  108. unsigned long inode_init(unsigned long start, unsigned long end)
  109. {
  110.     memset(hash_table, 0, sizeof(hash_table));
  111.     first_inode = NULL;
  112.     return start;
  113. }
  114.  
  115. static void __wait_on_inode(struct inode *);
  116.  
  117. static inline void wait_on_inode(struct inode * inode)
  118. {
  119.     if (inode->i_lock)
  120.         __wait_on_inode(inode);
  121. }
  122.  
  123. static inline void lock_inode(struct inode * inode)
  124. {
  125.     wait_on_inode(inode);
  126.     inode->i_lock = 1;
  127. }
  128.  
  129. static inline void unlock_inode(struct inode * inode)
  130. {
  131.     inode->i_lock = 0;
  132.     wake_up(&inode->i_wait);
  133. }
  134.  
  135. /*
  136.  * Note that we don't want to disturb any wait-queues when we discard
  137.  * an inode.
  138.  *
  139.  * Argghh. Got bitten by a gcc problem with inlining: no way to tell
  140.  * the compiler that the inline asm function 'memset' changes 'inode'.
  141.  * I've been searching for the bug for days, and was getting desperate.
  142.  * Finally looked at the assembler output... Grrr.
  143.  *
  144.  * The solution is the weird use of 'volatile'. Ho humm. Have to report
  145.  * it to the gcc lists, and hope we can do this more cleanly some day..
  146.  */
  147. void clear_inode(struct inode * inode)
  148. {
  149.     struct wait_queue * wait;
  150.  
  151.     wait_on_inode(inode);
  152.     remove_inode_hash(inode);
  153.     remove_inode_free(inode);
  154.     wait = ((volatile struct inode *) inode)->i_wait;
  155.     if (inode->i_count)
  156.         nr_free_inodes++;
  157.     memset(inode,0,sizeof(*inode));
  158.     ((volatile struct inode *) inode)->i_wait = wait;
  159.     insert_inode_free(inode);
  160. }
  161.  
  162. int fs_may_mount(dev_t dev)
  163. {
  164.     struct inode * inode, * next;
  165.     int i;
  166.  
  167.     next = first_inode;
  168.     for (i = nr_inodes ; i > 0 ; i--) {
  169.         inode = next;
  170.         next = inode->i_next;    /* clear_inode() changes the queues.. */
  171.         if (inode->i_dev != dev)
  172.             continue;
  173.         if (inode->i_count || inode->i_dirt || inode->i_lock)
  174.             return 0;
  175.         clear_inode(inode);
  176.     }
  177.     return 1;
  178. }
  179.  
  180. int fs_may_umount(dev_t dev, struct inode * mount_root)
  181. {
  182.     struct inode * inode;
  183.     int i;
  184.  
  185.     inode = first_inode;
  186.     for (i=0 ; i < nr_inodes ; i++, inode = inode->i_next) {
  187.         if (inode->i_dev != dev || !inode->i_count)
  188.             continue;
  189.         if (inode == mount_root && inode->i_count == 1)
  190.             continue;
  191.         return 0;
  192.     }
  193.     return 1;
  194. }
  195.  
  196. int fs_may_remount_ro(dev_t dev)
  197. {
  198.     struct file * file;
  199.     int i;
  200.  
  201.     /* Check that no files are currently opened for writing. */
  202.     for (file = first_file, i=0; i<nr_files; i++, file=file->f_next) {
  203.         if (!file->f_count || !file->f_inode ||
  204.             file->f_inode->i_dev != dev)
  205.             continue;
  206.         if (S_ISREG(file->f_inode->i_mode) && (file->f_mode & 2))
  207.             return 0;
  208.     }
  209.     return 1;
  210. }
  211.  
  212. static void write_inode(struct inode * inode)
  213. {
  214.     if (!inode->i_dirt)
  215.         return;
  216.     wait_on_inode(inode);
  217.     if (!inode->i_dirt)
  218.         return;
  219.     if (!inode->i_sb || !inode->i_sb->s_op || !inode->i_sb->s_op->write_inode) {
  220.         inode->i_dirt = 0;
  221.         return;
  222.     }
  223.     inode->i_lock = 1;
  224.     inode->i_sb->s_op->write_inode(inode);
  225.     unlock_inode(inode);
  226. }
  227.  
  228. static void read_inode(struct inode * inode)
  229. {
  230.     lock_inode(inode);
  231.     if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->read_inode)
  232.         inode->i_sb->s_op->read_inode(inode);
  233.     unlock_inode(inode);
  234. }
  235.  
  236. /*
  237.  * notify_change is called for inode-changing operations such as
  238.  * chown, chmod, utime, and truncate.  It is guaranteed (unlike
  239.  * write_inode) to be called from the context of the user requesting
  240.  * the change.    It is not called for ordinary access-time updates.
  241.  * NFS uses this to get the authentication correct.  -- jrs
  242.  */
  243.  
  244. int notify_change(int flags, struct inode * inode)
  245. {
  246.     if (inode->i_sb && inode->i_sb->s_op  &&
  247.         inode->i_sb->s_op->notify_change)
  248.         return inode->i_sb->s_op->notify_change(flags, inode);
  249.     return 0;
  250. }
  251.  
  252. /*
  253.  * bmap is needed for demand-loading and paging: if this function
  254.  * doesn't exist for a filesystem, then those things are impossible:
  255.  * executables cannot be run from the filesystem etc...
  256.  *
  257.  * This isn't as bad as it sounds: the read-routines might still work,
  258.  * so the filesystem would be otherwise ok (for example, you might have
  259.  * a DOS filesystem, which doesn't lend itself to bmap very well, but
  260.  * you could still transfer files to/from the filesystem)
  261.  */
  262. int bmap(struct inode * inode, int block)
  263. {
  264.     if (inode->i_op && inode->i_op->bmap)
  265.         return inode->i_op->bmap(inode,block);
  266.     return 0;
  267. }
  268.  
  269. void invalidate_inodes(dev_t dev)
  270. {
  271.     struct inode * inode, * next;
  272.     int i;
  273.  
  274.     next = first_inode;
  275.     for(i = nr_inodes ; i > 0 ; i--) {
  276.         inode = next;
  277.         next = inode->i_next;        /* clear_inode() changes the queues.. */
  278.         if (inode->i_dev != dev)
  279.             continue;
  280.         if (inode->i_count || inode->i_dirt || inode->i_lock) {
  281.             printk("VFS: inode busy on removed device %d/%d\n", MAJOR(dev), MINOR(dev));
  282.             continue;
  283.         }
  284.         clear_inode(inode);
  285.     }
  286. }
  287.  
  288. void sync_inodes(dev_t dev)
  289. {
  290.     int i;
  291.     struct inode * inode;
  292.  
  293.     inode = first_inode;
  294.     for(i = 0; i < nr_inodes*2; i++, inode = inode->i_next) {
  295.         if (dev && inode->i_dev != dev)
  296.             continue;
  297.         wait_on_inode(inode);
  298.         if (inode->i_dirt)
  299.             write_inode(inode);
  300.     }
  301. }
  302.  
  303. void iput(struct inode * inode)
  304. {
  305.     if (!inode)
  306.         return;
  307.     wait_on_inode(inode);
  308.     if (!inode->i_count) {
  309.         printk("VFS: iput: trying to free free inode\n");
  310.         printk("VFS: device %d/%d, inode %lu, mode=0%07o\n",
  311.             MAJOR(inode->i_rdev), MINOR(inode->i_rdev),
  312.                     inode->i_ino, inode->i_mode);
  313.         return;
  314.     }
  315.     if (inode->i_pipe) {
  316.         wake_up(&PIPE_READ_WAIT(*inode));
  317.         wake_up(&PIPE_WRITE_WAIT(*inode));
  318.     }
  319. repeat:
  320.     if (inode->i_count>1) {
  321.         inode->i_count--;
  322.         return;
  323.     }
  324.     wake_up(&inode_wait);
  325.     if (inode->i_pipe) {
  326.         unsigned long page = (unsigned long) PIPE_BASE(*inode);
  327.         PIPE_BASE(*inode) = NULL;
  328.         free_page(page);
  329.     }
  330.     if (inode->i_sb && inode->i_sb->s_op && inode->i_sb->s_op->put_inode) {
  331.         inode->i_sb->s_op->put_inode(inode);
  332.         if (!inode->i_nlink)
  333.             return;
  334.     }
  335.     if (inode->i_dirt) {
  336.         write_inode(inode);     /* we can sleep - so do again */
  337.         wait_on_inode(inode);
  338.         goto repeat;
  339.     }
  340.     inode->i_count--;
  341.     nr_free_inodes++;
  342.     return;
  343. }
  344.  
  345. struct inode * get_empty_inode(void)
  346. {
  347.     struct inode * inode, * best;
  348.     int i;
  349.  
  350.     if (nr_inodes < NR_INODE && nr_free_inodes < (nr_inodes >> 2))
  351.         grow_inodes();
  352. repeat:
  353.     inode = first_inode;
  354.     best = NULL;
  355.     for (i = 0; i<nr_inodes; inode = inode->i_next, i++) {
  356.         if (!inode->i_count) {
  357.             if (!best)
  358.                 best = inode;
  359.             if (!inode->i_dirt && !inode->i_lock) {
  360.                 best = inode;
  361.                 break;
  362.             }
  363.         }
  364.     }
  365.     if (!best || best->i_dirt || best->i_lock)
  366.         if (nr_inodes < NR_INODE) {
  367.             grow_inodes();
  368.             goto repeat;
  369.         }
  370.     inode = best;
  371.     if (!inode) {
  372.         printk("VFS: No free inodes - contact Linus\n");
  373.         sleep_on(&inode_wait);
  374.         goto repeat;
  375.     }
  376.     if (inode->i_lock) {
  377.         wait_on_inode(inode);
  378.         goto repeat;
  379.     }
  380.     if (inode->i_dirt) {
  381.         write_inode(inode);
  382.         goto repeat;
  383.     }
  384.     if (inode->i_count)
  385.         goto repeat;
  386.     clear_inode(inode);
  387.     inode->i_count = 1;
  388.     inode->i_nlink = 1;
  389.     nr_free_inodes--;
  390.     if (nr_free_inodes < 0) {
  391.         printk ("VFS: get_empty_inode: bad free inode count.\n");
  392.         nr_free_inodes = 0;
  393.     }
  394.     return inode;
  395. }
  396.  
  397. struct inode * get_pipe_inode(void)
  398. {
  399.     struct inode * inode;
  400.     extern struct inode_operations pipe_inode_operations;
  401.  
  402.     if (!(inode = get_empty_inode()))
  403.         return NULL;
  404.     if (!(PIPE_BASE(*inode) = (char*) __get_free_page(GFP_USER))) {
  405.         iput(inode);
  406.         return NULL;
  407.     }
  408.     inode->i_op = &pipe_inode_operations;
  409.     inode->i_count = 2;    /* sum of readers/writers */
  410.     PIPE_READ_WAIT(*inode) = PIPE_WRITE_WAIT(*inode) = NULL;
  411.     PIPE_HEAD(*inode) = PIPE_TAIL(*inode) = 0;
  412.     PIPE_RD_OPENERS(*inode) = PIPE_WR_OPENERS(*inode) = 0;
  413.     PIPE_READERS(*inode) = PIPE_WRITERS(*inode) = 1;
  414.     inode->i_pipe = 1;
  415.     inode->i_mode |= S_IFIFO | S_IRUSR | S_IWUSR;
  416.     inode->i_uid = current->euid;
  417.     inode->i_gid = current->egid;
  418.     inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  419.     return inode;
  420. }
  421.  
  422. struct inode * iget(struct super_block * sb,int nr)
  423. {
  424.     return __iget(sb,nr,1);
  425. }
  426.  
  427. struct inode * __iget(struct super_block * sb, int nr, int crossmntp)
  428. {
  429.     struct inode * inode, * empty;
  430.  
  431.     if (!sb)
  432.         panic("VFS: iget with sb==NULL");
  433.     empty = get_empty_inode();
  434. repeat:
  435.     inode = *(hash(sb->s_dev,nr));
  436.     while (inode) {
  437.         if (inode->i_dev != sb->s_dev || inode->i_ino != nr) {
  438.             inode = inode->i_hash_next;
  439.             continue;
  440.         }
  441.         wait_on_inode(inode);
  442.         if (inode->i_dev != sb->s_dev || inode->i_ino != nr)
  443.             goto repeat;
  444.         if (!inode->i_count)
  445.             nr_free_inodes--;
  446.         inode->i_count++;
  447.         if (crossmntp && inode->i_mount) {
  448.             struct inode * tmp = inode->i_mount;
  449.             iput(inode);
  450.             inode = tmp;
  451.             if (!inode->i_count)
  452.                 nr_free_inodes--;
  453.             inode->i_count++;
  454.             wait_on_inode(inode);
  455.         }
  456.         if (empty)
  457.             iput(empty);
  458.         return inode;
  459.     }
  460.     if (!empty)
  461.         return (NULL);
  462.     inode = empty;
  463.     inode->i_sb = sb;
  464.     inode->i_dev = sb->s_dev;
  465.     inode->i_ino = nr;
  466.     inode->i_flags = sb->s_flags;
  467.     put_last_free(inode);
  468.     insert_inode_hash(inode);
  469.     read_inode(inode);
  470.     return inode;
  471. }
  472.  
  473. /*
  474.  * The "new" scheduling primitives (new as of 0.97 or so) allow this to
  475.  * be done without disabling interrupts (other than in the actual queue
  476.  * updating things: only a couple of 386 instructions). This should be
  477.  * much better for interrupt latency.
  478.  */
  479. static void __wait_on_inode(struct inode * inode)
  480. {
  481.     struct wait_queue wait = { current, NULL };
  482.  
  483.     add_wait_queue(&inode->i_wait, &wait);
  484. repeat:
  485.     current->state = TASK_UNINTERRUPTIBLE;
  486.     if (inode->i_lock) {
  487.         schedule();
  488.         goto repeat;
  489.     }
  490.     remove_wait_queue(&inode->i_wait, &wait);
  491.     current->state = TASK_RUNNING;
  492. }
  493.